package ${package.Controller};

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import io.swagger.v3.oas.annotations.*;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import ${package.Entity}.Result;
import ${package.Entity}.StatusCode;
import org.springframework.web.bind.annotation.RestController;
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * ${table.comment!} 前端控制器
 * @author ${author}
 * @since ${date}
 */
<#if swagger>
@Tag(name = "${entity}接口文档")
</#if>
@RestController
@RequestMapping("/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

   @Autowired
   private ${table.serviceName} ${controllerMappingHyphen}Service;


    /***
    * 条件分页查询
    * @param ${controllerMappingHyphen}
    * @param page
    * @param size
    * @return
    */
    <#if swagger>
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    </#if>
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) <#if swagger>@Parameter(name = "${entity}对象",required = true)</#if> ${entity} ${controllerMappingHyphen}, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        Page<${entity}> pageInfo = ${controllerMappingHyphen}Service.page(new Page<${entity}>(page,size),new QueryWrapper<${entity}>(${controllerMappingHyphen}));
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }


    /***
    * 分页查询
    * @param page:当前页
    * @param size:每页显示多少条
    * @return
    */
    <#if swagger>
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    </#if>
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用${entity}Service实现分页查询${entity}
        Page<${entity}> pageInfo = ${controllerMappingHyphen}Service.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
    * 条件查询
    * @param ${controllerMappingHyphen}
    * @return
    */
    <#if swagger>
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @PostMapping(value = "/search" )
    public Result<List<${entity}>> findList(@RequestBody(required = false) <#if swagger>@Parameter(name = "${entity}对象",required = true)</#if> ${entity} ${controllerMappingHyphen}){
        List<${entity}> list = ${controllerMappingHyphen}Service.list(new QueryWrapper<${entity}>(${controllerMappingHyphen}));
        return new Result<List<${entity}>>(true,StatusCode.OK,"查询成功",list);
    }


    /***
    * 根据Id删除${controllerMappingHyphen}数据
    * @param id
    * @return
    */
    <#if swagger>
    @Operation(summary = "根据ID删除${entity}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    </#if>
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value="id") Integer id){
        ${controllerMappingHyphen}Service.removeById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }



    /***
    * 根据Id批量删除${controllerMappingHyphen}数据
    * @param ids
    * @return
    */
    <#if swagger==true>
    @Operation(summary = "根据Id批量删除${entity}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @DeleteMapping
    public Result deleteByIds(@RequestBody <#if swagger>@Parameter(name = "需要删除的${entity}对象id集合",required = true)</#if> List<Integer> ids){
        ${controllerMappingHyphen}Service.removeByIds(ids);
        return new Result(true,StatusCode.OK,"删除成功");
    }


    /***
    * 根据ID修改${entity}数据
    * @param ${controllerMappingHyphen}
    * @return
    */
    <#if swagger==true>
    @Operation(summary = "根据ID修改${entity}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    </#if>
    @PutMapping
    public Result updateById(@RequestBody <#if swagger>@Parameter(name = "${entity}对象",required = true)</#if> ${entity} ${controllerMappingHyphen}){
        ${controllerMappingHyphen}Service.updateById(${controllerMappingHyphen});
        return new Result(true,StatusCode.OK,"修改成功");
    }


     /***
     * 新增${entity}数据
     * @param ${controllerMappingHyphen}
     * @return
     */
     <#if swagger>
     @Operation(summary = "添加${entity}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     </#if>
     @PostMapping
     public Result add(@RequestBody  <#if swagger>@Parameter(name = "${entity}对象",required = true)</#if> ${entity} ${controllerMappingHyphen}){
        ${controllerMappingHyphen}Service.save(${controllerMappingHyphen});
        return new Result(true,StatusCode.OK,"添加成功");
     }

     /***
     * 根据ID查询${entity}数据
     * @param id
     * @return
     */
     <#if swagger>
      @Operation(summary = "根据ID查询${entity}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
      @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
     </#if>
     @GetMapping("/{id}")
     public Result<${entity}> findById(@PathVariable(value="id") Integer id){
        ${entity} ${controllerMappingHyphen} = ${controllerMappingHyphen}Service.getById(id);
        return new Result<${entity}>(true,StatusCode.OK,"查询成功",${controllerMappingHyphen});
     }

     /***
     * 根据Id批量查询${entity}数据
     * @param ids
     * @return
     */
     <#if swagger>
     @Operation(summary = "根据Id批量查询${entity}数据",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
     </#if>
     @GetMapping
     public Result<List<${entity}>> findByIds(@RequestBody <#if swagger>@Parameter(name = "需要查询的${entity}对象id集合",required = true)</#if> List<Integer> ids){
        List<${entity}> ${controllerMappingHyphen}List = ${controllerMappingHyphen}Service.listByIds(ids);
        return new Result<List<${entity}>>(true,StatusCode.OK,"查询成功",${controllerMappingHyphen}List);
     }

     /***
     * 查询${entity}全部数据
     * @return
     */
     <#if swagger==true>
     @Operation(summary = "查询所有${entity}",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     </#if>
     @GetMapping
     public Result<List<${entity}>> findAll(){
        List<${entity}> list = ${controllerMappingHyphen}Service.list();
        return new Result<List<${entity}>>(true, StatusCode.OK,"查询成功",list) ;
     }


}
</#if>
